package com.sf.dlb.controller;

import com.sf.dlb.bean.AppErrerBean;
import com.sf.dlb.constant.ResponseConstant;
import com.sf.dlb.jpa.model.TokenEntity;
import com.sf.dlb.service.AppErrerService;
import com.sf.dlb.utils.GsonUtils;
import com.sf.dlb.utils.ResultVOUtil;
import com.sf.dlb.vo.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * app错误日志表
 * Created by jing.tan on 2018/12/10.
 */
@RestController
@RequestMapping(value = "/v1/errer")
public class AppErrerController {
    private static Logger logger = LoggerFactory.getLogger(AppErrerController.class);

    @Autowired
    private AppErrerService appErrerService;
    /**
     * 添加错误
     *
     * @returns
     */
    @PostMapping(value = "/add")
    @ResponseBody
    public Object addErrer(HttpServletRequest request,@RequestBody AppErrerBean appErrerBean) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        logger.info("addErrer start ");
        if(StringUtils.isEmpty(appErrerBean.getAppType())){
            return ResultVOUtil.error(ResponseConstant.ERROR,"设备类型为空");
        }
        if(StringUtils.isEmpty(appErrerBean.getErrerLog())){
            return ResultVOUtil.error(ResponseConstant.ERROR,"错误日志为空");
        }
        TokenEntity tokenEntity=(TokenEntity)resultVO.getData();
        appErrerBean.setUserId(tokenEntity.getUserId());
        resultVO=appErrerService.addErrer(appErrerBean);
        logger.info("addErrer  end resultVO={} ",GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }

}
